exports.get = function(player, req, resp, onHandled) {
    var cloud_shop = player.user.cloud_shop;

    req.args.buy_count = cloud_shop.buy_count || 0;

    requestCloud(req, resp, function(){
        resp.data.score = cloud_shop.score;
        resp.data.buys = cloud_shop.buys;
        resp.data.exchange = cloud_shop.exchange;

        if (!cloud_shop.buy_count && resp.data.buy_count) {
            cloud_shop.buy_count = resp.data.buy_count;
            player.markDirty('cloud_shop.buy_count');
        }
        onHandled();
    });
}

exports.get_cloud_detail = function(player, req, resp, onHandled) {
    requestCloud(req, resp, onHandled);
}

exports.buy = function(player, req, resp, onHandled) {
    var cashCost = 0;
    do {
        if( isNaN(req.args.id) || isNaN(req.args.num) ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }
        if( Math.round(req.args.num) <= 0 || Math.round(req.args.num) > 100000 ) {
            resp.code = 1; resp.desc = 'invalid num'; break;
        }

        req.args.id = Math.round(req.args.id);
        req.args.num = Math.round(req.args.num);

        cashCost = req.args.num * gConfGlobal.CloudUnitPrice;
        if( player.user.status.cash < cashCost ) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }
    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    req.args.name = player.user.info.un;
    req.args.headpic = common.handleHeadpicParseException(player.user.info.headpic);

    requestCloud(req, resp, function(){
        if( resp.code == 0 && !resp.data.update ) {
            var times = resp.data.bought.times;
            var cloud_shop = player.user.cloud_shop;
            if( !cloud_shop.buys[times] ) {
                cloud_shop.buys[times] = [];
            }
            cloud_shop.buys[times] = cloud_shop.buys[times].concat(resp.data.bought.seqs);
            cloud_shop.score += gConfGlobal.CloudUnitPrice * req.args.num;
            cloud_shop.buy_count = +resp.data.buy_count;

            player.markDirty('cloud_shop.score');
            player.markDirty('cloud_shop.buys.'+times);
            player.markDirty('cloud_shop.buy_count');

            //@ToDo,控制buys长度

            player.costResource('cash', cashCost, req.args.id, req.args.num);
            resp.data.cash = -cashCost;
        }
        onHandled();
    });
}

exports.get_reward = function(player, req, resp, onHandled) {
    requestCloud(req, resp, function(){
        if( resp.code == 0 ) {
            resp.data.awards = player.addAwards(resp.data.awards);
        }
        onHandled();
    });
}

exports.get_wins = function(player, req, resp, onHandled) {
    requestCloud(req, resp, onHandled);
}

exports.my_buy = function(player, req, resp, onHandled) {
    req.args.bought = Object.keys(player.user.cloud_shop.buys);
    requestCloud(req, resp, onHandled);
}

exports.exchange = function(player, req, resp, onHandled) {
    do {
        var cloudShop = player.user.cloud_shop;
        var times = req.args.ten ? 10 : 1;

        if( cloudShop.score < (gConfGlobal.CloudLotteryScore * times) ) {
            resp.code = 1; resp.desc = 'not enough score'; break;
        }

        cloudShop.score -= gConfGlobal.CloudLotteryScore * times;

        var weights = {}; 
        for( var pos in gConfCloudLottery ) {
           weights[pos] = gConfCloudLottery[pos].Weight; 
        }

        var randPos = 1;
        var awards = [];
        for( var i=1; i<=times; i++ ) {
            randPos = +common.wRand(weights);
            awards.push(gConfCloudLottery[randPos].Award1);
        }
       
        for( var i=1; i<=times; i++ ) {
            cloudShop.exchange += 1;

            // 特殊奖励 
            if( cloudShop.exchange >= gConfGlobal.CloudLotteryLimit ) {
                awards.push( gConfCloudLottery[13].Award1);
                cloudShop.exchange = 0;
            }
        }

        player.markDirty('cloud_shop.score');
        player.markDirty('cloud_shop.exchange');

        resp.data.awards = player.addAwards(awards);
        resp.data.pos = randPos;
        resp.data.exchange = cloudShop.exchange;
    }while(false);

    onHandled();
}
